﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using LinqToLdap;
using LinqToLdap.Collections;

namespace LINQtoLDAPDemo_Model
{
    public class User
    {
        #region Properties
        public string DistinguishedName
        {
            get
            {
                return string.Format("CN={0} {1},CN=Users,{2}", this.FirstName, this.LastName, ConfigurationSettings.AppSettings["ldapPartition"]);
            }
            set { }
        }

        public string CN
        {
            get;
            set;
        }

        public string Manager { get; set; }
        public int EmployeeID { get; set; }
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public string Notes { get; set; }
        public string HomePhone { get; set; }
        public string Title { get; set; }
        public string TitleOfCourtesy { get; set; }
        public string DisplayName { get; set; }
        #endregion

        public static User[] GetAllUsers()
        {
            using (var ldapContext = ConfiguredDirectoryContext.DirectoryContext)
            {
                User[] allUsers = ldapContext.Query<User>().ToArray<User>();
                return allUsers;
            }
        }

        // TODO: Very similar to Group.GetAllGroups_PageTest()--clean up redundant code
        public static List<ILdapPage<User>> GetAllUsers_PageTest(int pageSize)
        {
            using (var ldapContext = ConfiguredDirectoryContext.DirectoryContext)
            {
                List<ILdapPage<User>> allUsers = new List<ILdapPage<User>>();
                LinqToLdap.Collections.ILdapPage<User> currentPage = null;

                do
                {
                    if (currentPage == null)
                    {
                        currentPage = ldapContext.Query<User>().ToPage(pageSize);
                    }
                    else
                    {
                        currentPage = ldapContext.Query<User>().ToPage(currentPage.PageSize, currentPage.NextPage);
                    }

                    allUsers.Add(currentPage);
                } while (currentPage.HasNextPage);

                return allUsers;
            }
        }

        public override string ToString()
        {
            return this.DisplayName;
        }
    }
}
